package cn.edu.zafu.bigdata.x.xiejunjie.fileoperations;

import java.io.*;
import java.util.*;

public class StudentManager {
    private List<Student> students;
    private String filePath = "students.dat";

    public StudentManager() {
        students = new ArrayList<>();
        loadStudents();
    }

    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            // If file not found or other error, just start with an empty list
        }
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (studentExists(id)) {
            System.out.println("学号已存在，请重新输入!");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.add(student);
        saveStudents();
        System.out.println("学生信息添加成功!");
    }

    public boolean studentExists(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public void searchStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入要查询的学号(输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) break;
            for (Student student : students) {
                if (student.getId().equals(id)) {
                    System.out.println(student);
                    return;
                }
            }
            System.out.println("未找到该学生!");
        }
    }

    public void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入要更新的学号(输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) break;
            for (int i = 0; i < students.size(); i++) {
                Student student = students.get(i);
                if (student.getId().equals(id)) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    student.setName(name);
                    student.setPhone(phone);
                    saveStudents();
                    System.out.println("学生信息更新成功!");
                    return;
                }
            }
            System.out.println("未找到该学生!");
        }
    }

    public void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入要删除的学号(输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) break;
            for (int i = 0; i < students.size(); i++) {
                if (students.get(i).getId().equals(id)) {
                    students.remove(i);
                    saveStudents();
                    System.out.println("学生信息删除成功!");
                    return;
                }
            }
            System.out.println("未找到该学生!");
        }
    }

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline left-over
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("程序退出...");
                    return;
                default:
                    System.out.println("无效的操作编号，请重新输入!");
            }
        }
    }

    public static void main(String[] args) {
        StudentManager manager = new StudentManager();
        manager.showMenu();
    }
}